package org.easymodelers.gmf.utils;

import java.io.IOException;
import java.net.MalformedURLException;

import org.eclipse.core.internal.utils.FileUtil;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;

public class GmfMappingToGenModelTransformer {

    /*public static boolean transformMapToGmfGenModel(GmfModel gmfModel,
                    boolean deleteGmfGenModel, String figureTemplatePath, IProgressMonitor monitor) {
            try {
                    IFile gmfGenModel = gmfModel.getGmfGenModelFile();
                    IFile gmfMapModel = gmfModel.getGmfMapModelFile();
                    IFile emfGenModel = gmfModel.getGenModelFile();
                    if (emfGenModel == null || gmfMapModel == null
                                    || gmfGenModel == null)
                            return false;
                    monitor
                                    .subTask("Creating GMF generator model from GMF mapping model");
                    gmfMapModel.getParent().refreshLocal(IResource.DEPTH_ONE, monitor);
                    if (deleteGmfGenModel && gmfGenModel.exists()) {
                            gmfGenModel.delete(true, monitor);
                    }
                    final ResourceSet rs = new ResourceSetImpl();
                    TransformToGenModelOperation op = new TransformToGenModelOperation(rs);
                    configureOptions(op.getOptions(), gmfModel);
                    if (figureTemplatePath != null) {
                            IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(figureTemplatePath);
                            if (res == null)
                                    throw new IOException("Folder " + figureTemplatePath + " not found");
                            op.getOptions().setFigureTemplatesPath(res.getLocationURI().toURL());
                    }
                    op.loadMappingModel(FileUtil.getURI(gmfMapModel), new NullProgressMonitor());
                    op.loadGenModel(FileUtil.getURI(emfGenModel), new NullProgressMonitor());
                    op.setGenURI(FileUtil.getURI(gmfGenModel));
                    IStatus status = op.executeTransformation(new NullProgressMonitor());
                    if (!status.isOK()) {
                            throw new CoreException(status);
                    }
                    gmfGenModel.refreshLocal(IResource.DEPTH_ONE, new NullProgressMonitor());
                    monitor.worked(1);
                    return true;
            } catch (CoreException ce) {
                    MessageDialog.openError(Display.getDefault().getActiveShell(),
                                    "Error transforming map model", ce.getMessage());
            } catch (MalformedURLException e) {
                    MessageDialog.openError(Display.getDefault().getActiveShell(),
                                    "Error transforming map model", e.getMessage());
            } catch (IOException e) {
                    MessageDialog.openError(Display.getDefault().getActiveShell(),
                                    "Error transforming map model", e.getMessage());
            }
            return false;
    }

    protected static void configureOptions(TransformOptions options, GmfModel gmfModel) {
            configureOptions(options);
            options.setGenerateRCP(gmfModel.isGenerateRCP());
    }

    protected static void configureOptions(TransformOptions options) {
            options.setUseRuntimeFigures(true);
            options.setUseMapMode(true);
            options.setGenerateRCP(false);
    }*/
}
